<#include "include/head.ftl">
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${NamespaceMapper}.I${Po}Mapper">

	<sql id="conditionsWhere">
		<#list table.columnList as column>
			<#if    column.columnName?uncap_first=="createDate"||column.columnName?uncap_first=="updateDate"  >
				<if test=" ${column.columnName?uncap_first} != null   ">
			      	AND   T1.${column.fieldName?capitalize} = ${"#"}{${column.columnName?uncap_first}}
				</if>
			<#else> 
				<if test=" ${column.columnName?uncap_first} != null  and  ${column.columnName?uncap_first}  !='' ">
						AND   T1.${column.fieldName?capitalize} = ${"#"}{${column.columnName?uncap_first}}
				</if>
			</#if>
	</#list>
		<if test=" likeName != null and likeName !='' ">
			AND  ( T1.Type_name like  '% ${"$"}{likeName}%'  or  T1.Type_code like  '% ${"$"}{likeName}%'   )
		</if>
	</sql>

	<sql id="selColumns">
		<#assign columnField>
		<#list table.columnList as column>
		 T1.${column.fieldName?capitalize} AS ${column.columnName?uncap_first},
		</#list>
		</#assign>
		${columnField?substring(0,
		columnField?last_index_of(","))}
	</sql>

	<select id="list" resultType="${NamespaceModel}.${Po}">
		SELECT
		<include refid="selColumns"></include>
		FROM ${tableName} T1
		
		order by T1.UPDATE DESC
	</select>

	<select id="listByEntity" resultType="${NamespaceModel}.${Po}">
		SELECT
		<include refid="selColumns"></include>
		FROM ${tableName} 	T1
		<where>
			<include refid="conditionsWhere"></include>
		</where>
		order by T1.UPDATE DESC
	</select>
	<select id="count" resultType="java.lang.Integer">
		SELECT COUNT(1) FROM ${tableName} T1
	</select>

	<select id="countByEntity" resultType="java.lang.Integer">
		SELECT COUNT(1) FROM ${tableName} T1
		<where>
			<!-- sql片段引入 -->
			<include refid="conditionsWhere"></include>
		</where>
	</select>

	<select id="get" parameterType="java.lang.String" resultType="${NamespaceModel}.${Po}">
		SELECT
		<include refid="selColumns"></include>
		FROM ${tableName} T1
		where id = ${"#"}${"{"}id${"}"}
	</select>

	<select id="getByEntity" resultType="${NamespaceModel}.${Po}">
		SELECT
		  <include refid="selColumns"></include>
		FROM ${tableName} T1
		<where>
			<!-- sql片段引入 -->
			<include refid="conditionsWhere"></include>
		</where>
	</select>

	<insert id="insert">
		INSERT INTO ${tableName}(
		<#assign insertField>
			<#list table.columnList as column>
			${column.fieldName?capitalize},
			</#list>
		</#assign>
		${insertField?substring(0, insertField?last_index_of(","))}
		) VALUES (
		<#assign insertJavaField>
			<#list table.columnList as column>
			 <#if   column.columnName?uncap_first=="createBy"||column.columnName?uncap_first=="updateBy"  >
		      	 ${"#"}{${column.columnName?uncap_first}.userId,jdbcType=VARCHAR}, 
			 <#else> 
				${"#"}{${column.columnName?uncap_first}},
			 </#if>
			</#list>
		</#assign>
		${insertJavaField?substring(0, insertJavaField?last_index_of(","))}
		)
	</insert>

	<update id="update">
		update ${tableName}  T1
		set <#assign updateField>		
			<#list table.columnList as column>
			<#if    column.columnName?uncap_first=="createDate"||column.columnName?uncap_first=="updateDate"  >
				<if test=" ${column.columnName?uncap_first} != null   ">
			      	, T1.${column.fieldName?capitalize} = ${"#"}{${column.columnName?uncap_first}} 
				</if>
		   <#elseif  column.columnName?uncap_first=="createBy"||column.columnName?uncap_first=="updateBy" >
				<if test=" ${column.columnName?uncap_first} != null   ">
			      , T1.${column.fieldName?capitalize} = ${"#"}{${column.columnName?uncap_first}.userId,jdbcType=VARCHAR}
				</if>
			<#else> 
				<if test=" ${column.columnName?uncap_first} != null  and  ${column.columnName?uncap_first}  !='' ">
					, T1.${column.fieldName?capitalize} = ${"#"}{${column.columnName?uncap_first}}
				</if>
			</#if>
			</#list>
			</#assign>
		${updateField}
		WHERE id = ${"#"}${"{"}id${"}"}
	</update>

	<delete id="remove">
		DELETE FROM ${tableName} 
		 WHERE ID = ${"#"}${"{"}id${"}"}
	</delete>

	<delete id="removeByEntity">
	  DELETE FROM ${tableName} T1
		<where>
			1！=1
			<!-- sql片段引入 -->
			<include refid="conditionsWhere"></include>
		</where>
	</delete>

</mapper>